﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraEditors.Drawing;
using DevExpress.Utils.Drawing;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace DevDemo.GridControlDemo
{
    public partial class GridControlsCheckALL : DevExpress.XtraEditors.XtraForm
    {
        int check = 0;

        public GridControlsCheckALL()
        {
            InitializeComponent();

            //创建一个空表
            DataTable dt = new DataTable();
            
            dt.Columns.Add("CHECKFLAG", System.Type.GetType("System.Int32"));

            dt.Columns.Add("Name", System.Type.GetType("System.String"));

            for (int i = 0; i < 5; i++)
            {
                dt.Rows.Add(0, "李四" + i);
            }

            //DataTable dt1 = dt.Clone();

            //DataRow dr = dt1.NewRow();

            //dr.ItemArray = dt.Rows[0].ItemArray;

            //dt1.Rows.Add(dr);

            this.gridControl_MOs.DataSource = dt;
        }

        private void gridView_MOs_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
        {
            if (e.Column != null && e.Column.FieldName == "CHECKFLAG")
            {
                e.Info.InnerElements.Clear();

                e.Painter.DrawObject(e.Info);

                CustomDrawCheckBox(e, check);

                e.Handled = true;
            }
        }

        public static void CustomDrawCheckBox(ColumnHeaderCustomDrawEventArgs e, int chk)
        {
            RepositoryItemCheckEdit repositoryItemCheckEdit = e.Column.ColumnEdit as RepositoryItemCheckEdit;
            if (repositoryItemCheckEdit != null)
            {
                System.Drawing.Graphics graphics = e.Graphics;
                System.Drawing.Rectangle bounds = e.Bounds;
                CheckEditViewInfo checkEditViewInfo = repositoryItemCheckEdit.CreateViewInfo() as CheckEditViewInfo;
                CheckEditPainter checkEditPainter = repositoryItemCheckEdit.CreatePainter() as CheckEditPainter;
                checkEditViewInfo.EditValue = chk;
                checkEditViewInfo.Bounds = bounds;
                checkEditViewInfo.CalcViewInfo(graphics);
                ControlGraphicsInfoArgs controlGraphicsInfoArgs = new ControlGraphicsInfoArgs(checkEditViewInfo, new GraphicsCache(graphics), bounds);
                checkEditPainter.Draw(controlGraphicsInfoArgs);
                controlGraphicsInfoArgs.Cache.Dispose();
            }
        }

        private void gridControl_MOs_Click(object sender, EventArgs e)
        {
            if (CheckValueChanged(this.gridView_MOs, "CHECKFLAG", check))
            {
                check = check == 0 ? 1 : 0;
            }

            gridView_MOs.UpdateCurrentRow();

            gridView_MOs.RefreshData();
        }

        public static bool CheckValueChanged(GridView gridView, string fieldName, int check)
        {
            bool flag = false;
            bool result;
            if (gridView != null)
            {

                gridView.ClearSorting();
                gridView.PostEditor();
                System.Drawing.Point pt = gridView.GridControl.PointToClient(System.Windows.Forms.Control.MousePosition);
                GridHitInfo gridHitInfo = gridView.CalcHitInfo(pt);
                if (gridHitInfo.InColumn && gridHitInfo.Column != null && gridHitInfo.Column.FieldName == fieldName)
                {

                    for (int i = 0; i < gridView.RowCount; i++)
                    {
                        gridView.SetRowCellValue(i, fieldName, (check == 0) ? 1 : 0);
                    }
                    result = true;
                    return result;
                }

            }
            result = flag;
            return result;
        }

        private void repositoryItemCheckEdit4_CheckedChanged(object sender, EventArgs e)
        {
            DataTable dt = gridControl_MOs.DataSource as DataTable;

            int selectIndex = gridView_MOs.FocusedRowHandle;

            if (selectIndex < 0) return;

            DataRow dr = dt.Rows[selectIndex];

            dr[0] = Convert.ToInt32(((DevExpress.XtraEditors.CheckEdit)sender).EditValue);

            DataRow[] drs = dt.Select("CHECKFLAG=1");

            if (drs.Length > 0 && drs.Length == dt.Rows.Count)
            {
                check = 1;
            }
            else
            {
                check = 0;
            }

            gridView_MOs.UpdateCurrentRow();

            gridView_MOs.RefreshData();
        }
    }
}